#include "usart_com.h"
#include "led.h"
#include "relay.h"

uint8_t recv_len = 0;
uint8_t recv_buf[5] = {0};
// 初始化usart0
void usart0_init(uint32_t baudval)
{
    // 时钟设置
    rcu_periph_clock_enable(RCU_USART0);
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_AF);
    // io设置
    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
    gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
    // 参数设置
    usart_deinit(USART0);
    usart_baudrate_set(USART0, baudval);
    usart_parity_config(USART0, USART_PM_NONE);
    usart_word_length_set(USART0, USART_WL_8BIT);
    usart_stop_bit_set(USART0, USART_STB_1BIT);
    // 使能发送和接收
    usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
    usart_receive_config(USART0, USART_RECEIVE_ENABLE);
    // 使能接收中断
    usart_interrupt_enable(USART0, USART_INT_RBNE);
    nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
    nvic_irq_enable(USART0_IRQn, 2U, 2U);
    // 使能usart0
    usart_enable(USART0);

    usart0_send_string((uint8_t *)"usart0_open_success!\n");
}
// 发送一个字节
void usart0_send_byte(uint8_t ch)
{
    usart_data_transmit(USART0, ch);
    while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
        ;
}
// 发送一个字符串
void usart0_send_string(uint8_t *ch)
{
    uint32_t i = 0;
    while (*(ch + i) != '\0')
    {
        usart0_send_byte(*(ch + i));
        i++;
    }
}
// 接收中断处理函数
void USART0_IRQHandler(void)
{
    uint8_t temp = 0;
    if (SET == usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE))
    {
        usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
        temp = usart_data_receive(USART0);
        if (temp == HEX_HEAD)
        {
            recv_len = 0;
            recv_buf[recv_len] = temp;
            recv_len++;
        }
        else if (temp == HEX_TAIL)
        {
            if (recv_len == 4)
            {
                recv_buf[recv_len] = temp;
                data_handle();
            }
            else
            {
                usart0_send_string((uint8_t *)"2usart0_recv_error!!!\n");
            }
        }
        else
        {
            if (recv_len > 0 && recv_len < 4)
            {
                recv_buf[recv_len] = temp;
                recv_len++;
            }
            else
            {
                usart0_send_string((uint8_t *)"1usart0_recv_error!!!\n");
            }
        }
    }
}
// 处理数据帧
void data_handle(void)
{
    switch (recv_buf[1])
    {
    case LED_ID:
        if (recv_buf[3] == LED_ON)
        {
            led_open(recv_buf[2]);
        }
        else if (recv_buf[3] == LED_OFF)
        {
            led_close(recv_buf[2]);
        }
        break;
    case RELAY_ID:
        if (recv_buf[3] == RELAY_CONNECT)
        {
            relay_connect();
        }
        else if (recv_buf[3] == RELAY_DISCONNECT)
        {
            relay_disconnect();
        }
        break;
    default:
        usart0_send_string((uint8_t *)"3usart0_recv_error!!!\n");
        break;
    }
}
